<?php

/**
 * 
 * 获取用户的文件列表
 * page(初始值为0) pageSize
 * 
 * LIMIT => pageSize
 * OFFSET => page * pageSize
 */


include './init_db.php';

$username = defaultGetData('username', '');
$password = defaultGetData('password', '');
$page = (int)defaultGetData('page', -1);
$pageSize = (int)defaultGetData('pageSize', 30);
$offset = (int)defaultGetData('offset', 0);


if (!$username || !$password) {
    die(json_encode(array(
        'code' => 999,
        'msg' => '参数缺失'
    )));
}


// 本程序支持page和offset方式获取数据，page方式优先，如果page为-1，则使用offset



if ($page >= 0) {
    $offset = $page * $pageSize;
}



$result = mysqli_query($conn, "SELECT * FROM `cloudDisk_user` WHERE `username` = '$username' AND `password` = '$password' LIMIT 1");
if (!mysqli_num_rows($result)) {
    die(json_encode(array(
        'code' => 1000,
        'msg' => '账号或密码错误'
    )));
}


// 获取容量信息
$used_capacity;
$total_capacity;
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    $used_capacity = (int)$row['used_capacity']; // 当前已用空间
    $total_capacity = (int)$row['total_capacity']; // 总空间
}

$result = mysqli_query($conn, "SELECT * FROM cloudDisk_file WHERE `creator` = '$username' ORDER BY `upload_time` DESC LIMIT $pageSize OFFSET $offset");
$data = array(
    'code' => 200,
    'page' => (int)$page,
    'pageSize' => (int)$pageSize,
    'result' => array(),
    'used_capacity' => $used_capacity,
    'total_capacity' => $total_capacity
);

$x = 0;
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    $data['result'][$x++] = $row;
}

echo json_encode($data);
